package com.example.demo.util;

import java.io.File;

import org.springframework.web.multipart.MultipartFile;

import com.example.demo.exception.SPIPException;

public class FileUtil {

	/**
	 * 文件上传
	 * 
	 * @param file
	 * @param filePath
	 *            D\\:image\\1.jpg
	 * @throws SPIPException
	 */
	public static void upload(MultipartFile file, String filePath) throws SPIPException {
		if (file.isEmpty()) {
			throw new SPIPException("上传失败，请选择文件");
		}
		File dest = new File(filePath);
		try {
			File fileParent = dest.getParentFile();  
			if(!fileParent.exists()){  
			    fileParent.mkdirs();  
			}
			file.transferTo(dest);
		} catch (Exception e) {
			throw new SPIPException("上传失败", e);
		}
	}
}
